"""
准确率: 94.73%
用KNN算法对鸢尾花分类
KNN由于依赖距离,所以需要标准化,别的算法则不需要
缺点:
    计算量大,内存开销大
"""
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

# 1 获取数据
iris = load_iris()
# 2 划分数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=6)
# 3 特征工程: 标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4 KNN算法预估器
estimator = KNeighborsClassifier()
estimator.fit(x_train,y_train)
# 5 模型评估
# 方法1: 直接对比真实值和预测值36/38 = 0.9473684210526315
y_predict = estimator.predict(x_test)
print(y_predict)
# 方法2: 计算准确率0.9473684210526315
score = estimator.score(x_test,y_test)
print(score)
# 6 手动输入数据进行测试
test = [[5.5, 2.8, 3.4, 1.3]]
res = estimator.predict(transfer.transform(test))
print(res)

